package com.wyzz.hr.web.servlet;

import java.io.File;
import java.util.HashMap;
import java.util.logging.Level;
import javax.servlet.ServletContext;
import org.eclipse.birt.core.exception.BirtException;
import org.eclipse.birt.core.framework.IPlatformContext;
import org.eclipse.birt.core.framework.Platform;
import org.eclipse.birt.core.framework.PlatformServletContext;
import org.eclipse.birt.report.engine.api.*;

import com.wyzz.hr.spring.SpringBeanFactory;
import com.wyzz.hr.util.MyTools;
import com.wyzz.hr.util.SysConfigManager;
import com.wyzz.hr.util.impl.PropertiesFileConfigManager;

public class BirtEngine {

	public BirtEngine() {
	}

	public static synchronized IReportEngine getBirtEngine(ServletContext sc) {
		if (birtEngine == null) {
			EngineConfig config = new EngineConfig();
			HashMap hm = config.getAppContext();
			hm.put("ctx", SpringBeanFactory.getSpringContext());
			hm.put("MyTools", new MyTools());
			hm.put("PropertiesReader", PropertiesFileConfigManager
					.getInstance());
			String logLevel = getPropertyFromConfigFile("logLevel");
			if (logLevel != null || logLevel.trim().length() > 0) {
				Level level = Level.OFF;
				if ("SEVERE".equalsIgnoreCase(logLevel))
					level = Level.SEVERE;
				else if ("WARNING".equalsIgnoreCase(logLevel))
					level = Level.WARNING;
				else if ("INFO".equalsIgnoreCase(logLevel))
					level = Level.INFO;
				else if ("CONFIG".equalsIgnoreCase(logLevel))
					level = Level.CONFIG;
				else if ("FINE".equalsIgnoreCase(logLevel))
					level = Level.FINE;
				else if ("FINER".equalsIgnoreCase(logLevel))
					level = Level.FINER;
				else if ("FINEST".equalsIgnoreCase(logLevel))
					level = Level.FINEST;
				else if ("OFF".equalsIgnoreCase(logLevel))
					level = Level.OFF;
				String logDir = (new StringBuilder()).append(
						sc.getRealPath("/")).append(
						getPropertyFromConfigFile("logDirectory")).toString();
				File logFile = new File(logDir);
				if (!logFile.exists())
					logFile.mkdirs();
				config.setLogConfig(logDir, level);
			}
			config.setEngineHome("");
			IPlatformContext context = new PlatformServletContext(sc);
			config.setPlatformContext(context);
			HTMLEmitterConfig emitterConfig = new HTMLEmitterConfig();
			emitterConfig.setActionHandler(new HTMLActionHandler());
			HTMLServerImageHandler imageHandler = new HTMLServerImageHandler();
			emitterConfig.setImageHandler(imageHandler);
			config.getEmitterConfigs().put("html", emitterConfig);
			try {
				Platform.startup(config);
				IReportEngineFactory factory = (IReportEngineFactory) Platform
						.createFactoryObject("org.eclipse.birt.report.engine.ReportEngineFactory");
				birtEngine = factory.createReportEngine(config);
			} catch (BirtException e) {
				e.printStackTrace();
			}
		}
		return birtEngine;
	}

	public static synchronized void destroyBirtEngine() {
		if (birtEngine == null) {
			return;
		} else {
			birtEngine.shutdown();
			Platform.shutdown();
			birtEngine = null;
			return;
		}
	}

	public Object clone() throws CloneNotSupportedException {
		throw new CloneNotSupportedException();
	}

	private static String getPropertyFromConfigFile(String key) {
		SysConfigManager manager = PropertiesFileConfigManager.getInstance();
		return manager.getProperty(key);
	}

	private static IReportEngine birtEngine = null;

}
